home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Processes / SubLaunch / sublaunch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-11  |  11.4 KB  |  429 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                            C Shell                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*                       09/02/87                       */
  6. /*______________________________________________________*/
  7.  
  8. #include    <CType.h>
  9. #include    <Quickdraw.h>
  10. #include    <Windows.h>
  11. #include    <OSUtils.h>
  12. #include    <Controls.h>
  13. #include    <desk.h>
  14. #include    <dialogs.h>
  15. #include    <Events.h>
  16. #include    <Memory.h>
  17. #include    <Menus.h>
  18. #include    <Notification.h>
  19. #include    <OSEvents.h>
  20. #include    <Packages.h>
  21. #include    <Palettes.h>
  22. #include    <Files.h>
  23. #include    <StandardFile.h>
  24. #include    <Resources.h>
  25. #include    <SANE.h>
  26. #include    <SegLoad.h>
  27. #include    <Sound.h>
  28. #include    <ToolUtils.h>
  29. #include    <StdIO.h>
  30. #include    <math.h>
  31.  
  32. extern _DataInit();
  33.  
  34. #define    TRUE            0xFF
  35. #define    FALSE            0
  36. #define    VERSION            1
  37. #define    SR_BIT            0
  38.  
  39. #define    appleID            128            
  40. #define    appleMenu        0
  41. #define    aboutMeCommand    1
  42.  
  43. #define    fileID            129
  44. #define    startCommand    1
  45. #define    quitCommand     2
  46.  
  47. #define    editID            130
  48.  
  49. #define    aboutMeDLOG        128
  50. #define    okButton        1
  51. #define    authorItem        2
  52. #define    languageItem    3
  53.  
  54. #define    nocolorID        130
  55. #define    no68020            133
  56. #define    no68881            132
  57. #define    no256            134
  58. #define    nosys6            135
  59.  
  60. typedef struct LaunchStruct {
  61.     char                  *pfName;     /* pointer to the name of launchee */
  62.     short int        param;
  63.     char            LC[2];     /*extended parameters:*/
  64.     long int        extBlockLen;/*number of bytes in extension == 6*/
  65.     short int        fFlags;     /*Finder file info flags (see below)*/
  66.     long int        launchFlags; /*bit 31,30==1 for sublaunch, others                             reserved*/
  67. } *pLaunchStruct;
  68.  
  69. pascal OSErr Launchit(pLaunchStruct pLnch) /* < 0 means error */
  70.     = {0x205F, 0xA9F2, 0x3E80};
  71.  
  72. long                Tick;
  73. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  74. Rect                screenRect, BaseRect, TotalRect, minRect;
  75. WindowPtr            whichWindow, myWindow;
  76. CTabHandle            mycolors;
  77. PaletteHandle        srcPalette;
  78. MenuHandle            mymenu1, mymenu2, mymenu0;
  79. EventRecord         myEvent;
  80. Boolean                DoneFlag;
  81. GDHandle            theGDevice;
  82. SysEnvRec            theWorld;
  83. OSErr                OSys;
  84.     
  85. /*______________________________________________________*/
  86. /*                What ever You want                    */
  87. /*______________________________________________________*/
  88. OSErr DoLaunch()
  89.  
  90. {  /* DoLaunch */       
  91.     struct LaunchStruct    myLaunch;
  92.     Point                wher;             /*where to display dialog*/
  93.     SFReply                   reply;           /*reply record*/
  94.     SFTypeList           myFileTypes;    /* we only want APPLs */        
  95.     short int               numFileTypes=1;
  96.     HFileInfo            myPB;
  97.     char                   *dirNameStr;
  98.     OSErr                err;
  99.     wher.h = 80;
  100.     wher.v = 90;
  101.     myFileTypes[0] = 'APPL'; /* we only want APPLs */ 
  102.     /*Let the user choose the file to Launch*/
  103.     SFGetFile(wher, "", nil, numFileTypes, myFileTypes, nil, &reply);
  104.  
  105.     if (reply.good)  
  106.     {
  107.         dirNameStr = &reply.fName;  /*initialize to file selected*/
  108.     
  109.     /*Get the Finder flags*/
  110.         myPB.ioNamePtr= dirNameStr;
  111.         myPB.ioVRefNum= reply.vRefNum;
  112.         myPB.ioFDirIndex= 0;
  113.         myPB.ioDirID = 0;
  114.           err = PBGetCatInfo((CInfoPBPtr) &myPB,false);
  115.         if (err != noErr)
  116.             return err;
  117.     
  118.     /*Set the current volume to where the target application is*/
  119.           err = SetVol(nil, reply.vRefNum);
  120.         if (err != noErr)
  121.             return err;
  122.     
  123.     /*Set up the launch parameters*/
  124.         myLaunch.pfName = &reply.fName; /*pointer to our fileName*/
  125.         myLaunch.param = 0; /*we don’t want alternate screen or sound                         buffers*/
  126.     /*set up LC so as to tell Launch that there is non-junk next*/
  127.          myLaunch.LC[0] = 'L'; myLaunch.LC[1] = 'C';
  128.          myLaunch.extBlockLen = 6; /*length of param. block past this                             long word*/
  129.     /*copy flags; set bit 6 of low byte to 1 for RO access:*/
  130.          myLaunch.fFlags = myPB.ioFlFndrInfo.fdFlags; /*from                                                   GetCatInfo*/
  131.          myLaunch.launchFlags = 0xC0000000; /*set BOTH hi bits for a                                 sublaunch*/
  132.     
  133.         err = Launchit(&myLaunch); /* do the sublaunch */
  134.         if (err < 0)
  135.         {
  136.         /* the launch failed, so put up an alert to inform the user */
  137.             /*DoLaunchFailed();*/
  138.             return err;
  139.         }
  140.         else
  141.             return noErr;
  142.     } /*if reply.good*/
  143. } /*DoLaunch*/
  144.  
  145. /*______________________________________________________*/
  146. /*                 About Prog Dialog                    */
  147. /*______________________________________________________*/
  148. void showAboutMeDialog()
  149. {
  150.     GrafPtr     savePort;
  151.     DialogPtr    theDialog;
  152.     short        itemHit;
  153.  
  154.     GetPort(&savePort);
  155.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  156.     SetPort(theDialog);
  157.  
  158.     do {
  159.         ModalDialog(nil, &itemHit);
  160.     } while (itemHit != okButton);
  161.  
  162.     CloseDialog(theDialog);
  163.  
  164.     SetPort(savePort);
  165.     return;
  166. }
  167.  
  168. /*______________________________________________________*/
  169. /*                 Do Menu Function                     */
  170. /*______________________________________________________*/
  171. void doCommand(mResult)
  172.     long    mResult;
  173. {
  174.     int                     theMenu, theItem;
  175.     char                    daName[256];
  176.     GrafPtr                 savePort;
  177.     OSErr                    err;
  178.  
  179.     theItem = LoWord(mResult);
  180.     theMenu = HiWord(mResult);
  181.     
  182.     switch (theMenu) {
  183. /*______________________________________________________*/
  184. /*                    Do Apple Menu                     */
  185. /*______________________________________________________*/
  186.         case appleID:
  187.             if (theItem == aboutMeCommand)
  188.                 showAboutMeDialog();
  189.             else {
  190.                 GetItem(mymenu0, theItem, daName);
  191.                 GetPort(&savePort);
  192.                 (void) OpenDeskAcc(daName);
  193.                 SetPort(savePort);
  194.             }
  195.             break;
  196. /*______________________________________________________*/
  197. /*                     Do File Menu                     */
  198. /*______________________________________________________*/
  199.         case fileID:
  200.             switch (theItem) {
  201.                 case startCommand:
  202.                     err = DoLaunch();
  203.                     break;
  204.                 case quitCommand:
  205.                     DoneFlag = TRUE;
  206.                     break;
  207.                 default:
  208.                     break;
  209.                 }
  210.             break;
  211. /*______________________________________________________*/
  212. /*                     Do Edit Menu                     */
  213. /*______________________________________________________*/
  214.         case editID:
  215.             switch (theItem) {
  216.                 default:
  217.                     break;
  218.                 }
  219.             break;
  220.         default:
  221.             break;
  222.         }
  223.     HiliteMenu(0);
  224.     return;
  225. }
  226.  
  227. /*______________________________________________________*/
  228. /*               Initialization traps                   */
  229. /*______________________________________________________*/
  230. void init()
  231. {
  232.     RgnHandle        tempRgn;
  233.     
  234.     UnloadSeg(_DataInit);
  235.     InitGraf(&qd.thePort);
  236.     FlushEvents(everyEvent, 0);
  237.     InitWindows();
  238.     InitDialogs(nil);
  239.     InitCursor();
  240. /*______________________________________________________*/
  241. /*            If not right Machine then stop            */
  242. /*______________________________________________________*/
  243.     OSys = SysEnvirons(VERSION,&theWorld);
  244.     /*if(!theWorld.hasColorQD) {
  245.         crashDia = GetNewDialog (nocolorID, nil, (WindowPtr) -1);
  246.         DrawDialog (crashDia);
  247.         Delay (300, tick);
  248.         ExitToShell();
  249.         }
  250.     if(theWorld.processor != env68020) {
  251.         crashDia = GetNewDialog (no68020, nil, (WindowPtr) -1);
  252.         DrawDialog (crashDia);
  253.         Delay (300, tick);
  254.         ExitToShell();
  255.         }
  256.     if(!theWorld.hasFPU) {
  257.         crashDia = GetNewDialog (no68881, nil, (WindowPtr) -1);
  258.         DrawDialog (crashDia);
  259.         Delay (300, tick);
  260.         ExitToShell();
  261.         }
  262.     if(theWorld.systemVersion < 0x0600) {
  263.         crashDia = GetNewDialog (nosys6, nil, (WindowPtr) -1);
  264.         DrawDialog (crashDia);
  265.         Delay (300, tick);
  266.         ExitToShell();
  267.         }*/
  268.             
  269. /*______________________________________________________*/
  270. /*                     Set Rects                        */
  271. /*______________________________________________________*/
  272.     screenRect = qd.screenBits.bounds;
  273.     offLeft = 0;
  274.     offTop = 0;
  275.     offRight = screenRect.right;
  276.     offBottom = screenRect.bottom;
  277.     SetRect(&BaseRect, 40, 60, 472, 282);
  278.     tempRgn = GetGrayRgn();
  279.     HLock ((Handle) tempRgn);
  280.     TotalRect = (**tempRgn).rgnBBox;
  281.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  282.                 (**tempRgn).rgnBBox.bottom - 40);
  283.     HUnlock ((Handle) tempRgn);
  284.  
  285. /*______________________________________________________*/
  286. /*        Open Window & set Palette & Picture           */
  287. /*______________________________________________________*/
  288.     theGDevice = GetMainDevice();
  289.     HLock ((Handle) theGDevice);
  290.     mycolors = (**(**theGDevice).gdPMap).pmTable;
  291.     numcolor = (**(**theGDevice).gdPMap).pixelSize;
  292.     HUnlock((Handle) theGDevice);
  293.     switch (numcolor) {
  294.         case 1:
  295.             numcolor = 2;
  296.             break;
  297.         case 2:
  298.             numcolor = 4;
  299.             break;
  300.         case 4:
  301.             numcolor = 16;
  302.             break;
  303.         case 8:
  304.             numcolor = 256;
  305.             break;
  306.         }
  307.     
  308.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  309.                             (WindowPtr) -1, TRUE, 150);
  310.     SetPort((WindowPtr) myWindow);
  311.     DrawGrowIcon (myWindow);
  312.  
  313.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  314.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  315.     
  316. /*______________________________________________________*/
  317. /*                    Set menus                         */
  318. /*______________________________________________________*/
  319.     mymenu0 = GetMenu(appleID);
  320.     AddResMenu(mymenu0, 'DRVR');
  321.     InsertMenu(mymenu0,0);
  322.     mymenu1 = newmenu(129,"File");
  323.     appendmenu(mymenu1,"Start;Quit");
  324.     InsertMenu(mymenu1,0);
  325.     mymenu2 = newmenu(130,"Edit");
  326.     InsertMenu(mymenu2,0);
  327.     DrawMenuBar();
  328.  
  329. /*______________________________________________________*/
  330. /*                  Init variables                      */
  331. /*______________________________________________________*/
  332.     DoneFlag = FALSE;
  333.     yieldTime = 0;
  334.     return;
  335. }
  336.  
  337. main()
  338. {
  339.     Boolean        track;
  340.     long        growResult;
  341. /*______________________________________________________*/
  342. /*                   Main Event loop                    */
  343. /*______________________________________________________*/
  344.     init();
  345.     for ( ;; ) {
  346.         if (DoneFlag) {
  347.             ExitToShell();
  348.             }
  349.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  350.             switch (myEvent.what) {
  351.                 case mouseDown:
  352.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  353.                         case inSysWindow:
  354.                             SystemClick(&myEvent, whichWindow);
  355.                             break;
  356.                         case inMenuBar:
  357.                             doCommand(MenuSelect(myEvent.where));
  358.                             break;
  359.                         case inContent:
  360.                             break;
  361.                         case inDrag:
  362.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  363.                             EraseRect (&whichWindow->portRect);
  364.                             DrawGrowIcon (whichWindow);
  365.                             break;
  366.                         case inGrow:
  367.                             growResult = GrowWindow (whichWindow, myEvent.where,
  368.                                                     &minRect);
  369.                             SizeWindow(whichWindow, LoWord(growResult), 
  370.                                     HiWord(growResult), TRUE);
  371.                             EraseRect (&whichWindow->portRect);
  372.                             DrawGrowIcon (whichWindow);
  373.                             break;
  374.                         case inGoAway:
  375.                             track = TrackGoAway (whichWindow, myEvent.where);
  376.                             if (track)
  377.                                 CloseWindow (whichWindow);
  378.                             break;
  379.                         case inZoomIn:
  380.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  381.                             if (track) {
  382.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  383.                                 EraseRect (&whichWindow->portRect);
  384.                                 DrawGrowIcon (whichWindow);
  385.                                 }
  386.                             break;
  387.                         case inZoomOut:
  388.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  389.                             if (track) {
  390.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  391.                                 EraseRect (&whichWindow->portRect);
  392.                                 DrawGrowIcon (whichWindow);
  393.                                 }
  394.                             break;
  395.                         default:
  396.                             break;
  397.                         }
  398.                     break;
  399.                 case keyDown:
  400.                     break;
  401.                 case autoKey:
  402.                     break;
  403.                 case updateEvt:
  404.                     if ((WindowPtr) myEvent.message == myWindow) {
  405.                         BeginUpdate((WindowPtr) myWindow);
  406.                         EndUpdate((WindowPtr) myWindow);
  407.                         }
  408.                     break;
  409.                 case diskEvt:
  410.                     break;
  411.                 case activateEvt:
  412.                     break;
  413.                 case app4Evt:
  414.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  415.                         yieldTime = 30;
  416.                         HideWindow((WindowPtr) myWindow);
  417.                         }
  418.                     else { /* Resume */
  419.                         yieldTime = 0;
  420.                         ShowWindow((WindowPtr) myWindow);
  421.                         SetPort((WindowPtr) myWindow);
  422.                         }
  423.                     break;
  424.                 default:
  425.                     break;
  426.                 }
  427.             }
  428.         }
  429. }